<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/9/2
 * Time: 15:13
 */
namespace App\Services;

use App\Repositories\BankSupportRepository;
use App\Repositories\PassageRepository;
use App\Repositories\UserLevelPassageRepository;

class PassageService extends CommonService
{
    protected $passageRepository;
    protected $userLevelPassageRepository;
    protected $bankSupportRepository;

    public function __construct(PassageRepository $passageRepository, UserLevelPassageRepository $userLevelPassageRepository,
                                BankSupportRepository $bankSupportRepository)
    {
        $this->passageRepository = $passageRepository;
        $this->userLevelPassageRepository = $userLevelPassageRepository;
        $this->bankSupportRepository = $bankSupportRepository;
    }

    /**
     * 获取通道列表
     * @param $type
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPassageList($type)
    {
        $data = $this->passageRepository->getListByType($type);
        //通道对应的会员使用费率与秒到费
        foreach ($data as $key=>$value) {
            $levelPassage = $this->userLevelPassageRepository->byPassageGet($value['id']);
            $data[$key]['to_level'] = $value;
            if (!empty($levelPassage)) {
                $data[$key]['to_level'] = $levelPassage;
            } else {
                $data[$key]['to_level'] = [];
            }
        }
        return $this->responseJson(0, $data);
    }

    /**
     * 获取支持的通道
     * @param $type
     * @param $bankCode
     * @return \Illuminate\Http\JsonResponse
     */
    public function getSupportPassage($type, $bankCode)
    {
        $data = [];
        if (1 == $type) {
            $bankSupport = $this->bankSupportRepository->byPassageIdAndBankCode(1, $bankCode);
            if (empty($bankSupport) || $bankSupport->is_support == 0) {
                $data[0]['passage_id'] = 1;
                $data[0]['is_support'] = 0;
            } else {
                $data[0]['passage_id'] = 1;
                $data[0]['is_support'] = 1;
            }
            $bankSupport = $this->bankSupportRepository->byPassageIdAndBankCode(3, $bankCode);
            if (empty($bankSupport) || $bankSupport->is_support == 0) {
                $data[1]['passage_id'] = 3;
                $data[1]['is_support'] = 0;
            } else {
                $data[1]['passage_id'] = 3;
                $data[1]['is_support'] = 1;
            }
        }
        if (2 == $type) {
            $bankSupport = $this->bankSupportRepository->byPassageIdAndBankCode(2, $bankCode);
            if (empty($bankSupport) || $bankSupport->is_support == 0) {
                $data[0]['passage_id'] = 2;
                $data[0]['is_support'] = 0;
            } else {
                $data[0]['passage_id'] = 2;
                $data[0]['is_support'] = 1;
            }
            $bankSupport = $this->bankSupportRepository->byPassageIdAndBankCode(4, $bankCode);
            if (empty($bankSupport) || $bankSupport->is_support == 0) {
                $data[1]['passage_id'] = 4;
                $data[1]['is_support'] = 0;
            } else {
                $data[1]['passage_id'] = 4;
                $data[1]['is_support'] = 1;
            }
        }
        return $this->responseJson(0, $data);
    }

    /**
     * 支持的银行列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function supportBankList($passageId)
    {
        $result = $this->bankSupportRepository->getPassagetToBank($passageId);
        return $this->responseJson(0, $result);
    }
}